﻿#include<QMutex>
#include<QMutexLocker>
#include"datebase.h"
#include"unitclass.h"
#include"systemfile.h"
#define INSERT_CAR_DATA   "INSERT INTO lb(c_id,weight_times,photo,weight_1,ccdh,csfsc,ccptp,cqjtu,ccttp,cqybh,dwlx) VALUES ('%1', '%2','%3','%4','%5','%6','%7','%8','%9','%10','%11')"
#define INSETT_AXIS_DATA  "INSERT INTO w_record(cId,csfsc,chdzz,ccx,cclx,cqybh,dwlx,ccdh,czs) VALUES ('%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8' ,'%9')"
#define INSERT_DY_WEIGHT_DATA "INSERT INTO w_record (cId,chdzz,csfsc,czs,cczsj,chdzz,ccx,cczl,cczzl) VALUES('%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8' ,'%9')"
#define SELECT_CAR_DATA   "select ccphm,cId from w_record order by cId desc LIMIT 1"
#define SELECT_FARTHEST   "select cId csfsc from w_record where csfsc = %1 order by cId desc LIMIT %2"
//#define SELECT_RECORD  "SELECT cId,ccphm,ccpys,czs,ccx,chdzz,cchzz,cczzl,cczl,ccc,cck,ccg,ccttp,ccptp,cqjtu,ccdh,cclx,cpId from w_record where csfsc='%1' order by cId desc"
#define SELECT_RECORD  "SELECT cId,ccphm,ccpys,czs,ccx,chdzz,cchzz,cczzl,cczl,ccc,cck,ccg,ccttp,ccptp,cqjtu,ccdh,cclx,cpId,cccdjbh,cqybh,dwlx,ccs,cpz,cjz,ccc,cck,ccg,cxszh,cyszh,ccyrdz,clxdh,czzhw,chclx,czzqy,cmdd,cjsyxm,cjsydh,cjsysfzh,cjsyzz,cjsyjszh,cjsycyzgzbh,cssdw,cssdwjgdm,cssdwdz,cssdwfzr,cssdwfzrdh,cssdwzcr,cczr,cczsj,ccdh,cclx,cbz from w_record where csfsc='%1' order by cId desc"

//query database to get RFID  c_id
#define QUERY_CARD_INFO "SELECT c_id,car_tare,max_weight from card_info WHERE c_id = '%1'"
#define IS_HAVE_CARDID "SELECT c_id FROM lb WHERE c_id = '%1' order by id desc LIMIT 1"
#define INSERT_NEW_CARD_AND_WEIGHT "INSERT INTO lb(c_id,weigth,time,is_down,status,tare,max_weight,car_number,num_axles,decs,anwt,car_shape) VALUES ('%1', '%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12')"
//first insert car info
#define INSERT_NEW_CAR_DATA "INSERT INTO lb(c_id,weight_times,weigth_1,time1,tare,rough,anwt,overweight,status,max_weight) VALUES('%1', %2, %3, '%4', %5, %6 ,%7, %8, %9, %10)"
//update car info
//#define UPDATE_CAR_INFO "UPDATE"


#define DELETE_BYID      "delete from w_record where  cId='%1'"
#define DELETE_FULLRECORD "delete from w_record where cId > '%1'"
//#define SELECT_TABLE "select cId,tableName from w_waitupload where tableName='w_record'"
#define SELECT_TABLE "select cId,tableName from w_waitupload"
#define DELETE_TABLE "delete from w_waitupload where cId='%1' and tableName='%2'"

#define INSERT_WAITUP_DATA   "INSERT INTO w_waitupload(cId,tableName) VALUES ('%1', '%2')"
QMutex mutex;
datebase *datebase::m_db = NULL;
QMutex datebase::m_mutex;
datebase *datebase::GetInstance()
{
    if (m_db == NULL) {
            QMutexLocker mutexLocker(&m_mutex);  // 自解锁
            if (m_db == NULL) {
                m_db = new datebase();
            }
        }
        return m_db;
}

datebase::datebase()
{
    mySql_db =QSqlDatabase::addDatabase("QMYSQL");
    qDebug()<<"datebase::datebase()";
}

// Find CardId to Insert Database
//"SELECT c_id,car_tare,max_weight from card_info WHERE c_id = '%1'"
bool datebase::IsQueryCardInfo(QSqlDatabase *db, CardInfo Info, QString card_id)
{
    QString stat=QString(QUERY_CARD_INFO)
            .arg(card_id);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    bool res=query.next();
    if(!res) {
        return false;
    }
   // Info.carId=query.value(0).toString();
    Info.carTare=query.value(1).toFloat();
    //Info.carMaxWeight=query.value(2).toFloat();
   // qDebug()<<"cadId="<<Info.carId<<"carTare="<<Info.carTare<<"carMaxWeight="<<Info.carMaxWeight;
    return true;
}

bool datebase::IsUpdateCardInfo(QSqlDatabase *db, QString card_id)
{
    QString stat=QString("SELECT c_id,weight_times,status FROM lb WHERE c_id = %1")
            .arg(card_id);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    bool res=query.next();
    if(!res) {
        return false;
    }
    int status=query.value(2).toInt();
    int weighttime=query.value(1).toInt();
    if(status==1 /*&& weighttime > 0*/)
    {
        qDebug()<<"Already Out Factory! Need to Insert new data";
        return false;  //Need not Update  but Insert;
    }
    if(status==3 && weighttime > 0)
    {
        //have data to compare last time data
        return true;
    }
}
//"INSERT INTO lb(c_id,weight_times,weight_1,time1,tare,rough,anwt,overweight,status) VALUES('%1', '%2', '%3', '%4', '%5', '%6' ,'%7', '%8', '%9')"
//bool datebase::InserNewCarInfo(QSqlDatabase *db, QString card_id, WeightInfo Info, CardInfo card_info)
//{
//    float RealWeight=Info.weightValue;//real weight
//    float MaxWeight=(float)card_info.carMaxWeight;//max weight
//    float RealTare=card_info.carTare;
//    float RealSuttle=RealWeight-RealTare;
//    float OverWeight;
//    qDebug()<<"RealWeight="<<RealWeight<<"MaxWeight="<<MaxWeight<<"RealTare="<<RealTare<<"RealSuttle="<<RealSuttle;
//    if(RealWeight > MaxWeight)
//    {
//        OverWeight= (RealWeight-MaxWeight)/MaxWeight;
//        QString Over= QString::number(OverWeight,'f',4);
//        OverWeight=Over.toFloat();
//    }
//    else
//        OverWeight = 0.0000;
//    QSqlQuery query(*db);
//    QString time=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
//    QString stat=QString(INSERT_NEW_CAR_DATA)
//                .arg(card_id)
//                .arg(1)
//                .arg(Info.weightValue)
//                .arg(time)
//                .arg(card_info.carTare)
//                .arg(Info.weightValue)
//                .arg(RealSuttle)
//                .arg(OverWeight)
//                .arg(3)
//                .arg(RealWeight);
//    if(!query.exec(stat))
//    {
//        qDebug()<<(stat)<<"\r\n";
//        qDebug()<<(query.lastError().text());
//        return false;
//    }
//    return true;
//}

bool datebase::UpdateCarInfo(QSqlDatabase *db, QString card_id, WeightInfo Info)
{
    QString stat=QString("SELECT c_id,weight_times FROM lb WHERE c_id = %1")
            .arg(card_id);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    bool res=query.next();
    if(!res) {
        return false;
    }
    int weighttime=query.value(1).toInt();
    QString time=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    QString Update=QString("UPDATE lb set weight_times=%1 ,weigth_%1=%2,time%1='%3' WHERE c_id=%4")
                        .arg(weighttime+1)
                        .arg(Info.weightValue)
                        .arg(time)
                        .arg(card_id);
    if(!query.exec(Update))
    {
        qDebug()<<(Update)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    qDebug()<<"Update Successful!";
    return true;
}

bool datebase::IsHaveCardId(QSqlDatabase *db, QString card_id)
{
    QString stat=QString(IS_HAVE_CARDID)
            .arg(card_id);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    bool res=query.next();
    if(!res) {
        return false;
    }
    return true;
}
//c_id,weigth,time,is_down,status,tare,max_weight,car_number,num_axles,decs,anwt,car_shape
bool datebase::InserNewCardAndWeight(QSqlDatabase *db, QString card_id, WeightInfo& info,CardInfo& car_info,CarType& car_type)
{
    QString time=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    QString stat=QString(INSERT_NEW_CARD_AND_WEIGHT)
            .arg(card_id)
            .arg(info.weightValue)
            .arg(time)
            .arg("2")
            .arg("3")
            .arg(car_info.carTare)
            .arg(car_type.max_weight)
            .arg(car_info.plate)
            .arg(car_type.car_axis)
            .arg(car_type.desc)
            .arg((float)(info.weightValue-car_info.carTare))
            .arg(car_type.car_shapes);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    bool res=query.next();
    if(!res)
    {
        return false;
    }
    return true;
}

bool datebase::IsNewInCar(QSqlDatabase *db, QString card_id)
{
    QString stat=QString("SELECT status FROM lb WHERE c_id='%1' order by id desc LIMIT 1").arg(card_id);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    query.next();
    int status=query.value(0).toInt();
    if(status==1)
        return true;
    return false;
}

bool datebase::IsDownWeight(QSqlDatabase *db,QString card_id,WeightInfo& info,CardInfo& car_info,CarType& car_type)
{
    QString over;
    float overWeight=info.weightValue-car_type.max_weight;
    float anwt=info.weightValue-car_info.carTare;
    if(overWeight<0)
        over="0";
    else
        over=QString::number(overWeight/(float)car_type.max_weight,'f',4);
    QString curtime=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    QString stat=QString("UPDATE lb SET time='%1',weigth='%2',overweight='%3',anwt='%4',is_down='2' WHERE c_id='%5' AND status<>1 order by id desc LIMIT 1")
                        .arg(curtime)
                        .arg(info.weightValue)
                        .arg(over)
                        .arg(anwt)
                        .arg(card_id);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
        return true;
}

bool datebase::FindUpWeightTimes(QSqlDatabase *db, QString card_id, int &times)
{
    QString stat=QString("SELECT weight_times FROM lb WHERE c_id='%1' AND status<>1 order by id desc LIMIT 1")
            .arg(card_id);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    query.next();
    //QString time1=query.value(0).toString();
    int time1=query.value(0).toInt();
    if(time1>0 && time1<10)
    {
        times=time1;
        return true;
    }
    return false;
}

bool datebase::SelectFirstTime(QSqlDatabase *db, QString card_id, QDateTime &datetime)
{
    QString stat=QString("SELECT time FROM lb WHERE c_id='%1' AND status<>1 order by id desc LIMIT 1")
            .arg(card_id);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    bool res=query.next();
    if(!res)
        return false;
    datetime=query.value(0).toDateTime();
    return true;
}

bool datebase::UpdateManyWeight(QSqlDatabase *db, QString card_id, WeightInfo Info)
{
    qDebug()<<"card_id="<<card_id<<"weight="<<Info.weightValue;
    QString curtime=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    QString stat=QString("UPDATE lb SET weigth='%1' ,time='%2',is_down='2' WHERE c_id='%3' order by id desc LIMIT 1")
            .arg(Info.weightValue)
            .arg(curtime)
            .arg(card_id);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    return true;
}




bool datebase::saveoneCarInfo(QSqlDatabase *db,CarInfStr carInfo)
{
    //增加timePoint时间节点，用于上传时查询
    QString sTime = carInfo.dt.toString("yyyyMMddhhmmsszzz");
    QString sqT=get_random_number();
    qDebug()<<"主键"<<sTime+sqT;
    QString stat = QString(INSERT_CAR_DATA)
            .arg(sTime+sqT)
            .arg(carInfo.plate)
            .arg(carInfo.plateColor)
            .arg(systemFile::GetInstance()->weightType)
            .arg(systemFile::GetInstance()->weightNum)//车道号/磅表编号
            .arg("0")
            .arg(carInfo.p1)
            .arg(carInfo.p2)
            .arg(carInfo.p2)
            .arg(systemFile::GetInstance()->sectionId)
            .arg(systemFile::GetInstance()->sectionType);
    QSqlQuery query(*db);
    if(!query.exec(stat)) {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    return true;
}

QString datebase::get_random_number()
{
    QTime time;
    time= QTime::currentTime();
    qsrand(time.msec()+time.second()*1000);
    int a=qrand()%9;
    int b=qrand()%9;
    int c=qrand()%9;
    int d=qrand()%9;
    int e=qrand()%9;
    int f=qrand()%9;
    QString xxx=QString("%1%2%3%4%5%6").arg (a).arg (b).arg (c).arg (d).arg (e).arg (f);
    return xxx;
}

bool datebase::updateOneCarInfo(QSqlDatabase *db,QString cpflag,QString cId,QString p)
{
    QSqlQuery query(*db);
    QString sql;
    if(cpflag=="cpt")
    {
        sql=QString("update w_record set ccptp='%1' where cId='%2'").arg(p).arg(cId);
    }else{
        sql=QString("update w_record set cqjtu='%1',ccttp='%2' where cId='%3'").arg(p).arg(p).arg(cId);
    }
    bool ret;
    query.clear();
    query.prepare(sql);
    ret=query.exec();
    if(ret)
    {
        qDebug()<<"updateOneCarInfo success";
        return true;
    }
    else
    {
        qDebug()<<"updateOneCarInfo fasle"<<query.lastError().text();
        return false;
    }
}


bool datebase::selectoneCarRecord(QSqlDatabase *db,QString car_no,QString &cid)
{
    QString stat = QString(SELECT_CAR_DATA);
    QSqlQuery query(*db);
    query.exec(stat);
    query.setForwardOnly(true);
    bool res=query.next();

    if(!res) {
        return false;
    }
    QString plate;
    plate=query.value(0).toString();
    cid=query.value(1).toString();
    qDebug()<<"selectoneCarRecord="<<SELECT_CAR_DATA<<"res="<<res<<car_no<<plate<<cid;
    if(car_no==plate)
    {
        return true;
    }else{
        return false;
    }
}

bool datebase::selectTable(QSqlDatabase *db,QString &recordId,QString &tableName)
{
    QString stat = QString(SELECT_TABLE);
    QSqlQuery query(*db);
    query.exec(stat);
    query.setForwardOnly(true);
    if(!query.next()) {
        return false;
    }
    recordId=query.value(0).toString();
    tableName=query.value(1).toString();
    qDebug()<<"selectTable"<<recordId<<tableName;
    return true;
}

bool datebase::deleteTable(QSqlDatabase *db,QString recordId,QString tableName)
{
    QString stat = QString(DELETE_TABLE).arg(recordId).arg(tableName);
    QSqlQuery query(*db);
    if(!query.exec(stat)) {
        qDebug()<<(stat);
        qDebug()<<query.lastError().text();
        return false;
    }
    qDebug()<<"stat="<<stat;
    return true;
}

bool datebase::selectOneRecord(QSqlDatabase *db,RecordInf *info,QString id)
{
    QString sql;
    bool res;
    sql = QString(SELECT_RECORD).arg(id);
    QSqlQuery query(*db);
    query.setForwardOnly(true);
    query.exec(sql);
    res=query.next();
    qDebug()<<"selectOneRecord="<<sql<<res;
    if(res)
    {
        QString fuban;
        int count;
        //SELECT cId,ccphm,ccpys,czs,ccx
        info->JiLuId=query.value(0).toString();
        info->ChePai=query.value(1).toString();
        info->ChePaiYanSe=query.value(2).toString();
        info->ZhouShu=query.value(3).toString();
        info->ZhouXing=query.value(4).toString();
        //,chdzz,cchzz,cczzl,cczl,ccc
        info->XianZhong=query.value(5).toString();
        info->MaoZhong=query.value(6).toString();
        info->ChaoZai=query.value(7).toString();
        info->ChaoZaiLv=query.value(8).toString();
        info->CheChang=query.value(9).toString();
        //,cck,ccg,ccttp,ccptp,cqjtu
        info->CheKuan=query.value(10).toString();
        info->CheGao=query.value(11).toString();
        info->CheTouTu=query.value(12).toString();
        info->ChePaiTu=query.value(13).toString();
        info->QuanJingTu=query.value(14).toString();
        //,ccdh,xxlx,cpId
        info->CheDaoHao=query.value(15).toString();
        info->ChengLeiXing=query.value(16).toString();
        fuban=query.value(17).toString();
        if(fuban.isEmpty())
        {
            info->FuBang=QString::number(BANG_ONE);
        }else{
            info->FuBang=QString::number(BANG_FU);
        }
        info->YuLiuTu1="";
        info->YuLiuTu2="";
        count=info->ZhouShu.toInt()+info->XianZhong.toInt()+info->MaoZhong.toInt();
        info->JiaoYanWei=QString::number(count);

        qDebug()<<"chepai<<"<<info->ChePaiTu;
        qDebug()<<"quanjingtu<<"<<info->QuanJingTu;

        //PC登记数据
        info->cccdjbh=query.value(18).toString();
        info->cqybh=query.value(19).toString();
        info->dwlx=query.value(20).toString();
        info->ccs=query.value(21).toString();
        info->cpz=query.value(22).toString();
        //
        info->cjz=query.value(23).toString();
        info->ccc=query.value(24).toString();
        info->cck=query.value(25).toString();
        info->ccg=query.value(26).toString();
        info->cxszh=query.value(27).toString();
        //
        info->cyszh=query.value(28).toString();
        info->ccyrdz=query.value(29).toString();
        info->clxdh=query.value(30).toString();
        info->czzhw=query.value(31).toString();
        info->chclx=query.value(32).toString();
        //
        info->czzqy=query.value(33).toString();
        info->cmdd=query.value(34).toString();
        info->cjsyxm=query.value(35).toString();
        info->cjsydh=query.value(36).toString();
        info->cjsysfzh=query.value(37).toString();
        //
        info->cjsyzz=query.value(38).toString();
        info->cjsyjszh=query.value(39).toString();
        info->cjsycyzgzbh=query.value(40).toString();
        info->cssdw=query.value(41).toString();
        info->cssdwjgdm=query.value(42).toString();
        //
        info->cssdwdz=query.value(43).toString();
        info->cssdwfzr=query.value(44).toString();
        info->cssdwfzrdh=query.value(45).toString();
        info->cssdwzcr=query.value(46).toString();
        info->cczr=query.value(47).toString();
        //
        info->cczsj=query.value(48).toString();
        info->ccdh=query.value(49).toString();
        info->cclx=query.value(50).toString();
        info->cbz=query.value(51).toString();

        return true;
    }else{
        qDebug()<<query.lastError().text();
        return false;
    }
}

bool datebase::updateOneRecord(QSqlDatabase *db,QString id,QString num)
{
    QSqlQuery query(*db);
    QString sql;
    sql=QString("update w_record set csfsc='%1' where cId='%2'").arg(num).arg(id);
    qDebug()<<"sql"<<sql;
    bool ret;
    query.clear();
    query.prepare(sql);
    ret=query.exec();
    if(ret)
    {
        qDebug()<<"updateAxis success";
        return true;
    }
    else
    {
        qDebug()<<"updateAxis fasle"<<query.lastError().text();
        return false;
    }
}
bool datebase::updateAxis(QSqlDatabase *db,AxisInfo *info)
{
    QSqlQuery query(*db),queryu(*db);
    bool ret;
    query.prepare("select cId from w_record where csfsc=0 order by cId desc LIMIT 1");
    query.exec();
    ret=query.next();
    qDebug()<<"ret="<<ret;
    if(ret)
    {
        QString cId,date,sql;
        cId=query.value(0).toString();
        date=cId.left(14);
        QDateTime date_one=QDateTime::fromString(date,"yyyyMMddhhmmss");
        if(date_one.secsTo(QDateTime::currentDateTime()) < 60)
        {
            QString Axis_value = QString::number(info->maxWeightValue, 'f', 2);
            sql=QString("update w_record set czs=%1,chdzz='%2',ccx='%3',csfsc=1 where cId='%4'").arg(info->axisNum).arg(Axis_value).arg(info->axisUserType).arg(cId);
            //qDebug()<<sql<<info->maxWeightValue<<info->axisUserType;
            queryu.clear();
            queryu.prepare(sql);
            ret=queryu.exec();
            if(ret)
            {
                qDebug()<<"updateAxis success";
                return true;
            }
            else
            {
                qDebug()<<"updateAxis fasle"<<queryu.lastError().text();
                return false;
            }
        }else//更新轮轴重新加入一条新的信息
        {
            QString sTime = QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz");
            QString sqT=get_random_number();
            QString Axis_value = QString::number(info->maxWeightValue, 'f', 2);
            QString stat = QString(INSETT_AXIS_DATA)
                    .arg(sTime+sqT)
                    .arg("1")
                    .arg(Axis_value)
                    .arg(info->axisUserType)
                    .arg(systemFile::GetInstance()->weightType)
                    .arg(systemFile::GetInstance()->sectionId)
                    .arg(systemFile::GetInstance()->sectionType)
                    .arg(systemFile::GetInstance()->weightNum)
                    .arg(info->axisNum);
            QSqlQuery query(*db);
            if(!query.exec(stat)) {
                qDebug()<<(stat)<<"\r\n";
                qDebug()<<"插入轮轴信息失败"<<(query.lastError().text());
                return false;
            }
            qDebug()<<"有图片但时间间隔比较长插入新的轮轴信息!";
            return true;
        }

    }else{
//        query.prepare("select cId from w_record order by cId desc LIMIT 1");
//        query.exec();
//        ret=query.next();
//        QString cId,date;
//        cId=query.value(0).toString();
//        date=cId.left(14);
//        QDateTime date_one=QDateTime::fromString(date,"yyyyMMddhhmmss");
//        if(date_one.secsTo(QDateTime::currentDateTime()) > 10)
//        {
//            return false;
//        }
        //没有图片信息，所有数据库里面也没有数据现在改成没有图片有轮轴就写数据库
        //增加timePoint时间节点，用于上传时查询
        QString sTime = QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz");
        QString sqT=get_random_number();
        QString Axis_value = QString::number(info->maxWeightValue, 'f', 2);
        qDebug()<<"没有图片但是有轮轴主键"<<sTime+sqT;
        QString stat = QString(INSETT_AXIS_DATA)
                .arg(sTime+sqT)
                .arg("1")
                .arg(Axis_value)
                .arg(info->axisUserType)
                .arg(systemFile::GetInstance()->weightType)
                .arg(systemFile::GetInstance()->sectionId)
                .arg(systemFile::GetInstance()->sectionType)
                .arg(systemFile::GetInstance()->weightNum)
                .arg(info->axisNum);
        QSqlQuery query(*db);
        if(!query.exec(stat)) {
            qDebug()<<(stat)<<"\r\n";
            qDebug()<<"插入轮轴信息失败"<<(query.lastError().text());
            return false;
        }
        qDebug()<<"没有图片先插入轮轴信息!";
        return true;
    }
}


bool datebase::updateWeight(QSqlDatabase *db,WeightInfo *info)
{
    QSqlQuery query(*db),queryu(*db);
    bool ret;
    query.prepare("select cId,chdzz from w_record where csfsc=1 order by cId desc LIMIT 1");
    query.exec();
    ret=query.next();
    qDebug()<<"ret="<<ret;
    if(ret)
    {
        QString cId,date,weight_fact,chaozlv;
        float weight_,cczl,fact_w,chaoZai;
        cId=query.value(0).toString();
        weight_=query.value(1).toFloat();
        int temp=info->weightValue*100;
        info->weightValue=(float)temp/100;
        weight_fact = QString::number(info->weightValue,'f',2);
        fact_w=weight_fact.toFloat();
        qDebug()<<"fact_w="<<fact_w<<"weith_"<<weight_;
        chaoZai=fact_w-weight_;
        cczl=(chaoZai)/weight_;
        qDebug()<<"cczl="<<cczl;
        chaozlv=QString::number(cczl, 'f', 4);
        if(chaozlv.toFloat()<=0)
            chaozlv="0";
        qDebug()<<"核定重量:"<<weight_<<"实际重量="<<weight_fact<<"超载率="<<chaozlv;
        date=cId.left(14);
        QString sql;
        sql=QString("update w_record set cchzz='%1',csfsc=2,cczl='%2',cczzl='%3',cczsj='%4' where cId='%5'").arg(weight_fact).arg(chaozlv).arg(chaoZai).arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(cId);
        //qDebug()<<"sql="<<sql;
        queryu.clear();
        queryu.prepare(sql);
        ret=queryu.exec();
        if(ret)
        {
            qDebug()<<"updateWeight success";
            return true;
        }
        else
        {
            qDebug()<<"updateWeight fasle"<<queryu.lastError().text();
            return false;
        }
    }else{
        return false;
    }
}

bool datebase::updateDynamicWeight(QSqlDatabase *db, DynamicWeightInfo *info ,AxisInfo* Ainfo)
{
    QSqlQuery query(*db),queryu(*db);
    bool ret;
    //找到有图片的数据  磅先于轮轴先到
    query.prepare("select cId from w_record where csfsc=0 order by cId desc LIMIT 1");
    query.exec();
    ret=query.next();
    qDebug()<<"ret="<<ret;
    if(ret)
    {
        QString cId;
        float cz,czl;
        cz=Ainfo->maxWeightValue-info->DyweightValue;
        if(cz>0)
           czl=0.00;
        else
           czl=cz/Ainfo->maxWeightValue;
        cId=query.value(0).toString();
        QString sql;
        sql=QString("update w_record set cchzz='%1',csfsc=2,czs='%2',cczsj='%3',chdzz='%4',ccx='%5',cczl='%6',cczzl='7%' where cId='%8'")
                .arg(info->DyweightValue)
                .arg(info->DyaxisNum)
                .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))
                .arg(Ainfo->maxWeightValue)
                .arg(Ainfo->axisUserType)
                .arg(cz)
                .arg(czl)
                .arg(cId);
        queryu.clear();
        queryu.prepare(sql);
        ret=queryu.exec();
        if(ret)
        {
            qDebug()<<"updateWeight Dy success";
            return true;
        }
        else
        {
            qDebug()<<"updateWeight Dy fasle"<<queryu.lastError().text();
            return false;
        }
    }else{
        QString sTime = QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz");
        QString sqT=get_random_number();
        //没有图片 自己插入
        float cz,czl;
        cz=Ainfo->maxWeightValue-info->DyweightValue;
        if(cz>0)
           czl=0.00;
        else
           czl=cz/Ainfo->maxWeightValue;
        //query.prepare("INSERT INTO w_record (cId,csfsc,chdzz,czs,cczsj,chdzz,ccx,cczl,cczzl) VALUES('%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8' ,'%9')
        QString stat = QString(INSERT_DY_WEIGHT_DATA)
                .arg(sTime+sqT)
                .arg("2")
                .arg(info->DyweightValue)
                .arg(info->DyaxisNum)
                .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))
                .arg(Ainfo->maxWeightValue)
                .arg(Ainfo->axisUserType)
                .arg(cz)
                .arg(czl);
        QSqlQuery query(*db);
        if(!query.exec(stat)) {
            qDebug()<<(stat)<<"\r\n";
            qDebug()<<"插入动态信息失败"<<(query.lastError().text());
            return false;
        }
        qDebug()<<"没有图片先插入动态信息!";
        return true;
    }
}

const char * datebase::StrToChar(QString str)
{
    QByteArray str_record;
    const char *out;
    str_record=str.toLocal8Bit();
    out=str_record.data();
    return out;
}

QByteArray datebase::Image_To_Base64(QString ImgPath)
{
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB2312"));
    QImage image(ImgPath);
    QByteArray ba;
    QBuffer buf(&ba);
    image.save(&buf,"JPG",20);
    QByteArray hexed = ba.toBase64();

    buf.close();
    qDebug()<<"Image_To_Base64"<<ImgPath<<hexed.length();
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    return hexed;
}


QString datebase::getXml(RecordInf *info)
{
    QString _str;
    QByteArray str,chepai,quanjing;
    chepai=Image_To_Base64(info->ChePaiTu);
    quanjing=Image_To_Base64(info->QuanJingTu);
    str="<?xml version='1.0' encoding='utf-8'?>\r\n";
    str.append("<root>\r\n");
    str.append("  <kjzc>\r\n");
    str.append(QString("    <cId>%1</cId>\r\n").arg(info->JiLuId));
    str.append(QString("    <cpId>%1</cpId>\r\n").arg(""));
    str.append(QString("    <cccdjbh>%1</cccdjbh>\r\n").arg(""));
    str.append(QString("    <cqybh>%1</cqybh>\r\n").arg(""));
    str.append(QString("    <dwlx>%1</dwlx>\r\n").arg(""));
    str.append(QString("    <ccphm>%1</ccphm>\r\n").arg(info->ChePai));
    str.append(QString("    <ccx>%1</ccx>\r\n").arg(info->ZhouXing));
    str.append(QString("    <czs>%1</czs>\r\n").arg(info->ZhouShu));
    str.append(QString("    <ccs>%1</ccs>\r\n").arg(""));
    str.append(QString("    <cpz>%1</cpz>\r\n").arg(""));
    str.append(QString("    <chdzz>%1</chdzz>\r\n").arg(info->XianZhong));
    str.append(QString("    <cchzz>%1</cchzz>\r\n").arg(info->MaoZhong));
    str.append(QString("    <cjz>%1</cjz>\r\n").arg(""));
    str.append(QString("    <cczzl>%1</cczzl>\r\n").arg(info->ChaoZai));
    str.append(QString("    <cczl>%1</cczl>\r\n").arg(info->ChaoZaiLv));
    str.append(QString("    <ccc>%1</ccc>\r\n").arg(info->CheChang));
    str.append(QString("    <cck>%1</cck>\r\n").arg(info->CheKuan));
    str.append(QString("    <ccg>%1</ccg>\r\n").arg(info->CheGao));
    str.append(QString("    <cxszh>%1</cxszh>\r\n").arg(""));
    str.append(QString("    <cyszh>%1</cyszh>\r\n").arg(""));
    str.append(QString("    <czzhw>%1</czzhw>\r\n").arg(""));
    str.append(QString("    <chclx>%1</chclx>\r\n").arg(""));
    str.append(QString("    <czzqv>%1</czzqv>\r\n").arg(""));

    str.append(QString("    <cmdd>%1</cmdd>\r\n").arg(""));
    str.append(QString("    <cjsyxm>%1</cjsyxm>\r\n").arg(""));
    str.append(QString("    <cjsydh>%1</cjsydh>\r\n").arg(""));
    str.append(QString("    <cjsysfzh>%1</cjsysfzh>\r\n").arg(""));
    str.append(QString("    <cjsyzz>%1</cjsyzz>\r\n").arg(""));
    str.append(QString("    <cjsyjszh>%1</cjsyjszh>\r\n").arg(""));
    str.append(QString("    <cjsycyzgzbh>%1</cjsycyzgzbh>\r\n").arg(""));
    str.append(QString("    <cssdw>%1</cssdw>\r\n").arg(""));
    str.append(QString("    <cssdwdz>%1</cssdwdz>\r\n").arg(""));
    str.append(QString("    <cssdwfzr>%1</cssdwfzr>\r\n").arg(""));
    str.append(QString("    <cssdwfzrdh>%1</cssdwfzrdh>\r\n").arg(""));
    str.append(QString("    <cssdwzcr>%1</cssdwzcr>\r\n").arg(""));
    str.append(QString("    <cczr>%1</cczr>\r\n").arg(""));
    str.append(QString("    <cczsj>%1</cczsj>\r\n").arg(""));
    str.append(QString("    <cclx>%1</cclx>\r\n").arg(""));

    str.append(QString("    <ccttp>%1</ccttp>\r\n").arg(""));
    str.append("    <ccptp>").append(chepai).append("</ccptp>\r\n");//车牌
    str.append("    <cqjtu>").append(quanjing).append("</cqjtu>\r\n");//全景

    str.append(QString("    <cjsztp>%1</cjsztp>\r\n").arg(""));
    str.append(QString("    <ccyzgztp>%1</ccyzgztp>\r\n").arg(""));
    str.append(QString("    <cxsztp>%1</cxsztp>\r\n").arg(""));
    str.append(QString("    <cdlysztp>%1</cdlysztp>\r\n").arg(""));
    str.append(QString("    <cjlzt>%1</cjlzt>\r\n").arg("0"));

    str.append("  </kjzc>\r\n");
    str.append("</root>\r\n");
    _str=QString(str);
    //qDebug()<<"getXml="<<_str;
    return _str;
}


bool datebase::saveWaitUpInfo(QSqlDatabase *db,QString cId,QString tabelaName)
{
    //增加timePoint时间节点，用于上传时查询
    QString stat = QString(INSERT_WAITUP_DATA)
            .arg(cId)
            .arg(tabelaName);
    QSqlQuery query(*db);
    if(!query.exec(stat)) {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    return true;
}


QString datebase::RecordXml(RecordInf *info)
{
    QString xmldoc,chePaiTu_bata,qunJingTu_bata,time_D;
    chePaiTu_bata=Image_To_Base64(info->ChePaiTu);
    qunJingTu_bata=Image_To_Base64(info->QuanJingTu);
    time_D=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    xmldoc = QString("<?xml version='1.0' encoding='utf-8'?><root>") +
            "<kjzc>" +
            "<cId>" + info->JiLuId + "</cId>" +
            "<cpId></cpId>" +
            "<cccdjbh>"+info->cccdjbh+"</cccdjbh>" +
            "<cqybh>" + info->cqybh + "</cqybh>" + //"<cqybh>" + "140222A111111" + "</cqybh>" +
            "<dwlx>" + info->dwlx + "</dwlx>" + //"<dwlx>" + "源头企业" + "</dwlx>" +
            "<ccphm>" + info->ChePai + "</ccphm>" +
            "<ccx>" + info->ZhouXing + "</ccx>" +
            "<czs>" + info->ZhouShu + "</czs>" +
            "<ccs>"+info->ccs+"</ccs>" +
            "<cpz>"+info->cpz+"</cpz>" +
            "<chdzz>" + info->XianZhong + "</chdzz>" +
            "<cchzz>" + info->MaoZhong + "</cchzz>" +
            "<cjz>"+info->cjz+"</cjz>" +
            "<cczzl>" + info->ChaoZai + "</cczzl>" +
            "<cczl>" + info->ChaoZaiLv + "</cczl>" +
            "<ccc>"+info->ccc+"</ccc>" +
            "<cck>"+info->cck+"</cck>" +
            "<ccg>"+info->ccg+"</ccg>" +
            "<cxszh>"+info->cxszh+"</cxszh>" +
            "<cyszh>"+info->cyszh+"</cyszh>" +
            "<ccyrdz>"+info->ccyrdz+"</ccyrdz>" +
            "<clxdh>"+info->clxdh+"</clxdh>" +
            "<czzhw>"+info->czzhw+"</czzhw>" +
            "<chclx>"+info->chclx+"</chclx>" + //"<chclx>散货</chclx>" +
            "<czzqy>"+info->czzqy+"</czzqy>" +
            "<cmdd>"+info->cmdd+"</cmdd>" +
            "<cjsyxm>"+info->cjsyxm+"</cjsyxm>" +
            "<cjsydh>"+info->cjsysfzh+"</cjsydh>" +
            "<cjsysfzh>"+info->cjsyzz+"</cjsysfzh>" +
            "<cjsyzz>"+info->cjsyzz+"</cjsyzz>" +
            "<cjsyjszh>"+info->cjsyjszh+"</cjsyjszh>" +
            "<cjsycyzgzbh>"+info->cjsycyzgzbh+"</cjsycyzgzbh>" +
            "<cssdw>"+info->cssdw+"</cssdw>" +
            "<cssdwjgdm>"+info->cssdwjgdm+"</cssdwjgdm>" +
            "<cssdwdz>"+info->cssdwdz+"</cssdwdz>" +
            "<cssdwfzr>"+info->cssdwfzr+"</cssdwfzr>" +
            "<cssdwfzrdh>"+info->cssdwfzrdh+"</cssdwfzrdh>" +
            "<cssdwzcr>"+info->cssdwzcr+"</cssdwzcr>" +
            "<cczr>" + info->cczr + "</cczr>" +//"<cczr>" + "操作员" + "</cczr>" +
            "<cczsj>" + time_D + "</cczsj>" +    //"<cczsj>" + time_D + "</cczsj>" +
            "<ccdh>"+info->ccdh+"</ccdh>" +
            "<cclx>"+info->cclx+"</cclx>" +
            "<ccttp>" + qunJingTu_bata + "</ccttp>" +
            "<ccptp>" + chePaiTu_bata + "</ccptp>" +
            "<cqjtu>" + qunJingTu_bata+ "</cqjtu>" +
            "<cjsztp></cjsztp>" +
            "<ccyzgztp></ccyzgztp>" +
            "<cxsztp></cxsztp>" +
            "<cdlysztp></cdlysztp>" +
            "<cjlzt>0</cjlzt>" +
            "<cbz>"+info->cbz+"</cbz>" +
            "</kjzc>" +
            "</root>";
    return xmldoc;
}

void datebase::compressImg(QString fileName)
{
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB2312"));
    QImage img;
    img.load(fileName);
    int wid,hei;
    wid=img.width();
    hei=img.height();
    QImage result = img.scaled(wid, hei,Qt::KeepAspectRatio);
    bool isSuccess = result.save("out.jpg", "JPEG", 100);
    if (!isSuccess)
    {
        qDebug() << "save image fail!";
    }
    QFile file("out.jpg");
    qint64 fsz = file.size();
    int quality = 100;
    while (fsz > 2048)
    {
        quality = quality - 5;
        isSuccess = result.save("out.jpg", "JPEG", quality);
        if (!isSuccess)
        {
            qDebug() << "save image fail! quality=" << quality;
        }
        fsz = file.size();
        qDebug() << "quality=" << quality;
        if (quality <= 0)
        {
            break;
        }
    }
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
}


void datebase::moveImageFile(QString filename)
{
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB2312"));
    QFile file(filename);
    if(file.exists())
    {
        file.remove();
    }
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
}

bool datebase::deleteone(QSqlDatabase *db,int Number)
{
   // QString select="select csfsc,cId from w_record order by cId LIMIT 10";
    QString select = QString(SELECT_FARTHEST).arg(4).arg(Number);
    QSqlQuery query(*db);
    mutex.lock();
    if(!query.exec(select)) {
        debugOut(select);
        debugOut(query.lastError().text());
        mutex.unlock();
        return false;
    }
    mutex.unlock();
    QStringList cid;
    while(query.next())
        cid.append(query.value(0).toString());
    for(int i=0;i<cid.size();i++)
    {
        QString stat = QString(DELETE_BYID).arg(cid.at(i));
        QSqlQuery querydel(*db);
        mutex.lock();
        if(!querydel.exec(stat)) {
            debugOut(stat);
            debugOut(querydel.lastError().text());
            mutex.unlock();
            return false;
        }
        mutex.unlock();
   }
    return true;
}

QString datebase::GetIp(QSqlDatabase *db)
{
    QString selectIp="SELECT IP FROM u_subuser WHERE LOGINNAME='admin'";
    QSqlQuery query(*db);
    mutex.lock();
    if(!query.exec(selectIp)) {
        debugOut(selectIp);
        debugOut(query.lastError().text());
        mutex.unlock();
        //return false;
    }
    mutex.unlock();
    QString cid;
    while(query.next())
        cid=query.value(0).toString();
    qDebug()<<"cid="<<cid;
    return cid;
}


bool datebase::UpdateOutFactoryStatus(QSqlDatabase *db, QString card_id)
{
    QString stat=QString("UPDATE lb SET status='7' WHERE c_id='%1' order by id desc LIMIT 1")
            .arg(card_id);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    bool res=query.next();
    if(!res)
        return false;
    return true;
}


bool datebase::UpdateImage(QSqlDatabase *db, const QString& card_id, const QString &imagePath, bool IsOut)
{
    QString currtime=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    QString stat;
    if(!IsOut)
    {
        stat=QString("UPDATE lb SET photo='%1' WHERE c_id='%2' order by id desc LIMIT 1")
                .arg(imagePath)
                .arg(card_id);
    }
    else
    {
        stat=QString("UPDATE lb SET is_down='%1', factoryphoto='%2',factorytime='%3' WHERE c_id='%4' AND status=1 order by id desc LIMIT 1")
                .arg(1)
                .arg(imagePath)
                .arg(currtime)
                .arg(card_id);
    }
    qDebug()<<"stat"<<stat;
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    bool res=query.next();
    if(!res)
        return false;
    return true;
}

bool datebase::IsOutFactory(QSqlDatabase *db, const QString &card_id)
{
    QString stat;
    stat=QString("SELECT status FROM lb WHERE c_id='%1' order by id desc LIMIT 1")
            .arg(card_id);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    if(query.next())
    {
        int status=query.value(0).toInt();
        if(status>1)
        {
            if(status==7)
                return false;
            return true;
        }
        else
            return false;
    }
//   // qDebug()<<"Out Factory plate="<<plate;
    return false;
}

bool datebase::SureOut(QSqlDatabase *db, const QString& card_id)
{
    QString stat;
    stat=QString("UPDATE lb SET status='%1' WHERE c_id='%2' order by id desc LIMIT 1")
            .arg(1)
            .arg(card_id);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    bool res=query.next();
    if(!res)
        return false;
//    QString plate=query.value(0).toString();
    return true;
}

bool datebase::IsPrintList(QSqlDatabase *db, QString card_id)
{
    QString stat;
    stat=QString("SELECT status FROM lb WHERE c_id='%1' order by id desc LIMIT 1")
            .arg(card_id);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    query.next();
    int m_status=query.value(0).toInt();
    if(m_status!=4)
        return true;
    else
        return false;
//    QString plate=query.value(0).toString();
    return true;
}

bool datebase::SureDown(QSqlDatabase *db, QString card_id)
{
    QString stat;
    stat=QString("UPDATE lb SET is_down=%1 WHERE c_id='%2' order by id desc LIMIT 1")
            .arg(1)
            .arg(card_id);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    bool res=query.next();
    if(!res)
        return false;
    return true;
}

bool datebase::IsHaveUnusualCar(QSqlDatabase *db, const QString &car_plate)
{
    QString stat;
    stat=QString("SELECT car_number FROM lb WHERE car_number='%1' order by id desc LIMIT 1")
            .arg(car_plate);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    if(query.next())
    {
        return true;
    }
    return false;
}

bool datebase::UpdateUnusualCar(QSqlDatabase *db, WeightInfo &info,const QString &car_plate)
{
    QString stat;
    stat=QString("UPDATE lb SET is_down='2',weigth='%1' WHERE car_number='%2' order by id desc LIMIT 1")
            .arg(info.weightValue)
            .arg(car_plate);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    return true;
}

bool datebase::UpdateUnusualPic(QSqlDatabase *db, const QString &card_id, const QString &imagePath)
{
    QString currtime=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    QString stat;
    stat=QString("UPDATE lb SET photo='%1',time='%2' WHERE car_number='%3' order by id desc LIMIT 1")
            .arg(imagePath)
            .arg(currtime)
            .arg(card_id);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    bool res=query.next();
    if(!res)
        return false;
    return true;
}
//INSERT INTO lb(c_id,weigth,time,is_down,status,tare,max_weight,car_number,num_axles,decs,anw
bool datebase::InsertUnusualCar(QSqlDatabase *db, WeightInfo &info, const QString &car_plate)
{
    QString stat;
    stat=QString("INSERT INTO lb(car_number,weigth,is_down) VALUES('%1','%2','%3')")
            .arg(car_plate)
            .arg(info.weightValue)
            .arg(2);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    bool res=query.next();
    if(!res)
    {
        return false;
    }
    return true;
}

bool datebase::UnusualCarDown(QSqlDatabase *db, const QString &card_id)
{
    QString stat;
    stat=QString("UPDATE lb SET is_down='1' WHERE car_number='%1' order by id desc LIMIT 1")
            .arg(card_id);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    bool res=query.next();
    if(!res)
        return false;
    return true;
}
bool datebase::isSkinNewCarWeight(QSqlDatabase * db, QString CardId)
{
	QString stat;
    stat=QString("SELECT status FROM car_tare WHERE car_number='%1'")
            .arg(CardId);
	QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    bool res=query.next();
    if(!res)
        return false;
    return true;
}
bool datebase::insertSkinCardWeight(QSqlDatabase * db, QString CardId, WeightInfo& info)
{
	QString currtime=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
	QString stat;
    stat=QString("INSERT into car_type(car_number, sj_tare, status, intime) values('%1', '%2', '%3', '%4')")
                .arg(CardId)
                .arg(info.weightValue)
                .arg(2)          //在磅上的状态为2  
                .arg(currtime);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    bool res=query.next();
    if(!res)
        return false;
    return true;
}
bool datebase::isSkinSetFlag(QSqlDatabase *db, QString CardId)
{
    QString stat;
    stat=QString("select status from car_tare where id = (select max(id) from car_tare)");
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    QString res;
    while(query.next())
        res=query.value(0).toString();
    if(2 == res.toInt())
        return false;
    return true;
}

bool datebase::updateSkinWeight(QSqlDatabase * db, QString CardId, WeightInfo& info)
{
	QString currtime=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
	QString stat;
    stat=QString("UPDATE car_type set sj_tare = '%1' where car_number = '%2'")
                .arg(info.weightValue)
                .arg(CardId);
	QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    bool res=query.next();
    if(!res)
        return false;
    return true;
}
bool datebase::isSkinHaveNone(QSqlDatabase * db)
{
	QString stat;
    stat=QString("select car_number from car_tare where id = (select max(id) from car_tare)");
	QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    QString res;
    while(query.next())
        res=query.value(0).toString();
    if(res != "no")
        return false;
    return true;
}
bool datebase::insertNoneCarWht(QSqlDatabase *db, WeightInfo &info)
{
    QString currtime=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    QString stat;
    stat=QString("INSERT into car_tare(car_number, sj_tare, status, intime) values('%1', '%2', '%3', '%4')")
                .arg("no")
                .arg(info.weightValue)
                .arg(2)          //在磅上的状态为2
                .arg(currtime);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    bool res=query.next();
    if(!res)
        return false;
    return true;
}
bool datebase::undateNoneCarWht(QSqlDatabase *db, WeightInfo& info)
{
    QString currtime=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    QString stat;
    stat=QString("UPDATE lb set weigth = %1, time = '%2', is_down = 2 where car_number = 'no'")
                .arg(info.weightValue)
                .arg(currtime);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    bool res=query.next();
    if(!res)
        return false;
    return true;
}
bool datebase::isINbangFlag(QSqlDatabase *db)
{
    QString stat;
    stat=QString("select status from car_tare where id = (select max(id) from car_tare)");
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    QString res;
    while(query.next())
        res=query.value(0).toString();
    if(1 == res.toInt())
        return false;
    return true;
}
//INSERT INTO lb(
bool datebase::insertHeadNewCar(QSqlDatabase *db, QString card_id, WeightInfo& info,CardInfo& car_info,CarType& car_type)
{
    QString time=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    QString stat=QString("INSERT INTO lb(c_id,time,is_down,status,car_tare,tare,max_weight,car_number,num_axles,decs,car_shape) VALUES ('%1', '%2','%3','%4','%5','%6','%7','%8','%9','%10','%11')")
            .arg(card_id)
            .arg(time)
            .arg("2")
            .arg("3")
            .arg(info.weightValue)
            .arg(car_info.carTare)
            .arg(car_type.max_weight)
            .arg(car_info.plate)
            .arg(car_type.car_axis)
            .arg(car_type.desc)
            .arg(car_type.car_shapes);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    bool res=query.next();
    if(!res)
    {
        return false;
    }
    return true;
}
bool datebase::updateHeadNewCar(QSqlDatabase *db, QString CardId, WeightInfo &info)
{
    QString stat=QString("UPDATE lb SET car_tare = '%1' WHERE c_id='%2' order by id desc LIMIT 1")
                        .arg(info.weightValue)
                        .arg(CardId);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
        return true;
}
bool datebase::IsUpdateCarTare(QSqlDatabase *db, QString CardId)
{
    QString stat;
    stat=QString("select status from lb where c_id = '%1' order by id desc LIMIT 1")
            .arg(CardId);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    QString res;
    while(query.next())
        res=query.value(0).toString();
    if(res.isEmpty())
        qDebug() << "status is Empty" << endl;
    if(3 == res.toInt())
        return false;
    return true;
}
bool datebase::insertLbNoneCarTare(QSqlDatabase *db, WeightInfo &info)
{
    QString currtime=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    QString stat;
    stat=QString("INSERT into lb (car_number, is_down, weigth, status, time) values('%1', '%2', '%3', '%4', '%5')")
                .arg("no")
                .arg("2")
                .arg(info.weightValue)
                .arg(3)
                .arg(currtime);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    bool res=query.next();
    if(!res)
        return false;
    return true;
}
bool datebase::isHaveNone(QSqlDatabase *db)
{
    QString stat;
    stat=QString("select car_number from lb where car_number = 'no'");
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    bool res=query.next();
    if(!res)
        return false;
    return true;
}
bool datebase::updateLbNoneCarTare(QSqlDatabase *db, WeightInfo &info)
{
    QString stat=QString("UPDATE lb SET weigth = '%1'' WHERE car_number = 'no'")
                        .arg(info.weightValue);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    bool res=query.next();
    if(!res)
        return false;
    return true;
}
bool datebase::isSetTareFlag(QSqlDatabase *db, QString CardId)
{

}
bool datebase::InSetDownCar(QSqlDatabase *db, QString CardId)
{
    QString stat=QString("UPDATE lb SET is_down = 1 WHERE c_id = '%1'")
                        .arg(CardId);
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    bool res=query.next();
    if(!res)
        return false;
    return true;
}
bool datebase::InSetDownNoneCar(QSqlDatabase *db)
{
    QString stat=QString("UPDATE lb SET is_down =1 WHERE car_number = 'no'");
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    bool res=query.next();
    if(!res)
        return false;
    return true;
}

bool datebase::ClearIsDown(QSqlDatabase *db)
{
    QString stat=QString("UPDATE lb SET is_down =1 WHERE is_down = '2' ");
    QSqlQuery query(*db);
    if(!query.exec(stat))
    {
        qDebug()<<(stat)<<"\r\n";
        qDebug()<<(query.lastError().text());
        return false;
    }
    bool res=query.next();
    if(!res)
        return false;
    return true;
}
